﻿using System;
using System.Drawing;
using System.Windows.Forms;
using AiFrame.InterfaceLib.Windows.Controls;
using AiFrame.Windows.Controls.DataNavigationBar.MVP;

namespace AiFrame.Windows.Controls.DataNavigationBar
{
    public partial class DataNavigationBar : UserControl, IDataNavigationBar
    {
        private IDataNavigationBarTheme _navigationBarTheme;
        private const int WINDOW_WIDTH = 170;
        private DataNavigationBarPresenter _presenter;
        private bool _showHeader;

        public DataNavigationBar(IDataNavigationBarTheme theme)
        {
            _presenter = new DataNavigationBarPresenter(this, theme);
            _navigationBarTheme = theme;
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            topPictureBox.Image = _navigationBarTheme.GetDataNavBarHeaderImage();
            base.Width = WINDOW_WIDTH;
            //PaintGradient();
            contentPanel.BackColor = Color.White;
            
        }

        public bool ShowHeader
        {
            get
            {
                return _showHeader;
            }
            set
            {
                _showHeader = value;
                topPanel.Visible = value;
            }
        }

        public Control GetButtonContainer()
        {
            return contentPanel;
        }

        #region Implementation of IDataNavigationBar

        public void AddButton(string name, string caption, 
                Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc)
        {
            _presenter.OnAddButton(name, caption, normalImage, hoveredImage, proc);
        }

        public void AddButton(string name, string caption, Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc,
            object saveObject, int positionIndex)
        {
            _presenter.OnAddButton(name, caption, normalImage, hoveredImage, proc, saveObject, positionIndex);
        }


        public void SetParent(Control parent)
        {
            this.Parent = parent;
        }

        public void SetDockingStyle(DockStyle dockingStyle)
        {
            this.Dock = dockingStyle;
        }

        #endregion

        private void PaintGradient()
        {
            /* Fenster wird nicht richtig aktualisiert, das hier wird erstmal
             * deaktiviert */

//            System.Drawing.Drawing2D.LinearGradientBrush gradientBrush;
//            gradientBrush = new LinearGradientBrush(new Point(0, 0),
//                new Point(0, contentPanel.Height),
//                _navigationBarTheme.GetDataNavBarGradientStartColor(),
//                _navigationBarTheme.GetDataNavBarGradientEndColor());
//            Bitmap bmp = new Bitmap(contentPanel.Width, contentPanel.Height);
//            Graphics g = Graphics.FromImage(bmp);
//            g.FillRectangle(gradientBrush, new Rectangle(0, 0, contentPanel.Width,
//                contentPanel.Height));
//            contentPanel.BackgroundImage = bmp;
//            contentPanel.BackgroundImageLayout = ImageLayout.Stretch;
            
        }

        private void topPictureBox_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Font font = 
                new Font("Microsoft Sans Serif", 9, FontStyle.Bold);
                e.Graphics.DrawString("Navigation", font, new SolidBrush(Color.Black),
                4, 5);
        }

        private void DataNavigationBar_Resize(object sender, EventArgs e)
        {
            _presenter.OnSizeChanged();
        }
        
        public new int Width
        {
            get
            {
                return base.Width;
            }
        }

        public void BringWindowToFront()
        {
            this.BringToFront();
        }

        public void RemoveAllButtons()
        {
            _presenter.RemoveAllButtons();
        }
    }
}
